/*******************************************************************************
 *
 * Copyright (c) 2008-2011 ayound@gmail.com
 * This program and the accompanying materials
 * are made available under the terms of the GNU GENERAL PUBLIC LICENSE
 * which accompanies this distribution, and is available at
 * http://dev.perl.org/licenses/gpl1.html
 * All rights reserved.
 *
 * Created on 2009-11-10
 *******************************************************************************/

package org.ayound.skin.editor.core.convert.image;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import net.jmge.gif.DirectGif89Frame;
import net.jmge.gif.Gif89Encoder;

import org.apache.commons.io.FileUtils;
import org.ayound.skin.editor.core.ColorFillFilter;
import org.ayound.skin.editor.core.HSBFilter;
import org.ayound.skin.editor.core.ImageUtils;
import org.ayound.skin.editor.core.convert.AbstractConverter;
import org.ayound.skin.editor.core.convert.model.SkinModel;

public class ImageConverter extends AbstractConverter {

	private int imageType = BufferedImage.TYPE_INT_ARGB;

	private ColorModel colorModel = null;

	private WritableRaster raster = null;



	public ImageConverter(SkinModel model) {
		super(model);
	}

	public void convert(String filePath) {
		File fromFile = new File(filePath);
		File tempFile = new File(System.getProperty("java.io.tmpdir"),fromFile.getName());
		try {
			FileUtils.copyFile(fromFile, tempFile);
			convert(tempFile.getAbsolutePath(), filePath);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(tempFile!=null){
				tempFile.delete();
			}
		}

	}

	public void convert(String srcFile, String type,OutputStream output){
		if ("gif".equalsIgnoreCase(type)) {
			GifDecoder decoder = new GifDecoder();
			decoder.read(srcFile);
			Gif89Encoder encode = new Gif89Encoder();
			encode.setLoopCount(decoder.getLoopCount());
			for(int i=0;i<decoder.getFrameCount();i++){
				try {
					DirectGif89Frame gifFrame = new DirectGif89Frame(filterImage(decoder.getFrame(i)));
					gifFrame.setDelay(decoder.getDelay(i));
					encode.addFrame(gifFrame);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			try {
				encode.encode(output);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			try {
				BufferedImage srcImage = ImageIO.read(new File(srcFile));
				colorModel = srcImage.getColorModel();
				raster = srcImage.getRaster();
				if(srcImage.getType()!=0){
					imageType = srcImage.getType();
				}
				saveImage(filterImage(srcImage), type,output);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		try {
			output.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void convert(String srcFile, String toFile)
			throws FileNotFoundException {
		File file = new File(toFile);
		String type = file.getName().substring(
				file.getName().lastIndexOf('.') + 1);
		convert(srcFile,type,new FileOutputStream(toFile));
	}

	public Image filterImage(Image image) {
		HSBFilter hsbFilter = new HSBFilter();
		hsbFilter.setHue(getModel().getHue());
		hsbFilter.setSaturation(getModel().getSaturation());
		hsbFilter.setBrightness(getModel().getBrightness());
		Image newImage = image;
		newImage = ImageUtils.filterImage(hsbFilter, newImage);
		ColorFillFilter fillFilter = new ColorFillFilter();
		fillFilter.setRed(getModel().getRed());
		fillFilter.setBlue(getModel().getBlue());
		fillFilter.setGreen(getModel().getGreen());
		return ImageUtils.filterImage(fillFilter, newImage);

	}

	public void saveImage(Image image, File file) {
		String type = file.getName().substring(
				file.getName().lastIndexOf('.') + 1);
		try {
			saveImage(image, type, new FileOutputStream(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public void saveImage(Image image, String type, OutputStream output) {
		BufferedImage bi = null;
		if(image instanceof BufferedImage){
			bi = (BufferedImage)image;
		}else{
			int width = image.getWidth(null);
			int height = image.getHeight(null);
			bi = new BufferedImage(width, height,
					imageType);
			Graphics2D g2 = bi.createGraphics();
			g2.drawImage(image, 0, 0, null);
			g2.dispose();
		}
		try {
			ImageIO.write(bi, type, output);
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		try {
			output.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SkinModel model = new SkinModel();
		// for (int i = 0; i <= 100; i++) {
		model.setHue(50);
		ImageConverter c = new ImageConverter(model);
		try {
//			c.convert("D:\\ext-2.0.2\\air\\resources\\icons\\extlogo128.png", "D:\\ext-2.0.2\\air\\resources\\icons\\extlogo1281.png");
			c.convert("d:\\shadow.png", "d:\\shadow1.png");
//			c.convert("d:\\test.bmp", "d:\\test1.bmp");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// }


	}
}
